﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Portal_Tech;

namespace Portal_Tech
{
    public partial class Form1 : Form
    {
        SubGroup group = new SubGroup();
        //Lists holding all groups and floats
        List<SubGroup> groupList;
        List<ParadeFloat> floatList;
        //misc bools
        bool firstRun = true;
        bool changesMade = false;
        //for save/load function
        Serializer serializer = new Serializer();
        
       
     
        public Form1()
        {
            floatList = new List<ParadeFloat>();
            for (int i = 0; i < 15; i++)
            {
                floatList.Add(new ParadeFloat());
                floatList[i].floatNumber = i;
            }


            InitializeComponent();
        }

        private void update()
        {
            int x = 0;
            label3.Text = Convert.ToString(floatList[0].numberOfRiders);
            label4.Text = Convert.ToString(floatList[1].numberOfRiders);
            label5.Text = Convert.ToString(floatList[2].numberOfRiders);
            label6.Text = Convert.ToString(floatList[3].numberOfRiders);
            label7.Text = Convert.ToString(floatList[4].numberOfRiders);
            label8.Text = Convert.ToString(floatList[5].numberOfRiders);
            label9.Text = Convert.ToString(floatList[6].numberOfRiders);
            label10.Text = Convert.ToString(floatList[7].numberOfRiders);
            label11.Text = Convert.ToString(floatList[8].numberOfRiders);
            label12.Text = Convert.ToString(floatList[9].numberOfRiders);
            label13.Text = Convert.ToString(floatList[10].numberOfRiders);
            label14.Text = Convert.ToString(floatList[11].numberOfRiders);
            label15.Text = Convert.ToString(floatList[12].numberOfRiders);
            label16.Text = Convert.ToString(floatList[13].numberOfRiders);
            label17.Text = Convert.ToString(floatList[14].numberOfRiders);


            var listBoxes = Form1.ActiveForm.Controls.OfType<ListBox>();
            foreach (ListBox box in listBoxes)
                box.Items.Clear();
            
            foreach (SubGroup sub in floatList[0].groups)
            {
                listBox1.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[1].groups)
            {
                listBox2.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[2].groups)
            {
                listBox3.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[3].groups)
            {
                listBox4.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[4].groups)
            {
                listBox5.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[5].groups)
            {
                listBox6.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[6].groups)
            {
                listBox7.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[7].groups)
            {
                listBox8.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[8].groups)
            {
                listBox9.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[9].groups)
            {
                listBox10.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[10].groups)
            {
                listBox11.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[11].groups)
            {
                listBox12.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[12].groups)
            {
                listBox13.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[13].groups)
            {
                listBox14.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }
            foreach (SubGroup sub in floatList[14].groups)
            {
                listBox15.Items.Add((sub.GroupID + "   :" + sub.numberInGroup));
            }

            foreach (ParadeFloat p in floatList)
            {
                x += p.numberOfRiders;
            }

            
        }

        // Reevaluate
        private void button1_Click(object sender, EventArgs e)
        {
            if (firstRun)
            {
                groupList = new List<SubGroup>();
                Test.generateTestData(groupList);

            }
            else if (groupList == null)
                groupList = new List<SubGroup>();
            changesMade = true;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
            {
                label18.Text = "0";
            }
            else if (groupList != null)
            {
                foreach (SubGroup x in groupList)
                {
                    if (Convert.ToInt32(textBox2.Text) == x.GroupID)
                    {
                        label18.Text = Convert.ToString(x.numberInGroup);
                        break;
                    }
                    else
                        label18.Text = "0";
                }
            }
            else
                label18.Text = "0";
        }

        // Year end
        private void button4_Click(object sender, EventArgs e)
        {
            changesMade = true;

            foreach (ParadeFloat p in floatList)
            {
                p.numberOfRiders = 0;
                p.groups.Clear();
            }

            if (groupList == null)
            {
                MessageBox.Show("There are no Floats.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }

            if (firstRun)
            {
                List<SubGroup> notAdded = Distribution.Distribute(floatList, groupList, 15, 1);
                firstRun = false;
            }
            else
            {
                List<SubGroup> notAdded = Distribution.FrontToBack(groupList, floatList);
            }
            update();
        }


        // Save
        private void button2_Click(object sender, EventArgs e)
        {
            if (!serializer.saveGroups(groupList))
            {
                MessageBox.Show("Save of group list could not be completed.", "ERROR",
                    MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }

            else if (serializer.saveFloats(floatList))
            {
                MessageBox.Show("Save completed successfully.", "Portal Tech",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Save of float list could not be completed.", "ERROR",
                    MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            changesMade = false;
        }

        //Load
        private void button3_Click(object sender, EventArgs e)
        {
            groupList = serializer.readGroups();
            if(groupList == null)
                MessageBox.Show("Load of group list could not be completed.", "ERROR",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            floatList = serializer.readFloats();
            if(floatList == null)
                MessageBox.Show("Load of float list could not be completed.", "ERROR",
                    MessageBoxButtons.OK, MessageBoxIcon.Hand);
            else
                MessageBox.Show("Load completed successfully.", "Portal Tech",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            update();
            changesMade = false;
        }

        private void exit_button_Click(object sender, EventArgs e)
        {
            if (changesMade)
            {
                switch (MessageBox.Show("Exitting... Save progress?", "!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Hand))
                {
                    case DialogResult.No:
                        Application.Exit();
                        break;
                    case DialogResult.Yes:
                        button2_Click(sender, e);
                        Application.Exit();
                        break;
                    case DialogResult.Cancel:
                        return;
                }
            }
            else
                Application.Exit();
        }

        private void listBox15_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox14_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox13_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox12_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox11_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox10_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox9_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox8_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox7_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox6_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox5_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label34_Click(object sender, EventArgs e)
        {

        }

        private void label33_Click(object sender, EventArgs e)
        {

        }

        private void label32_Click(object sender, EventArgs e)
        {

        }

        private void label31_Click(object sender, EventArgs e)
        {

        }

        private void label30_Click(object sender, EventArgs e)
        {

        }

        private void label29_Click(object sender, EventArgs e)
        {

        }

        private void label28_Click(object sender, EventArgs e)
        {

        }

        private void label27_Click(object sender, EventArgs e)
        {

        }

        private void label26_Click(object sender, EventArgs e)
        {

        }

        private void label25_Click(object sender, EventArgs e)
        {

        }

        private void label24_Click(object sender, EventArgs e)
        {

        }

        private void label23_Click(object sender, EventArgs e)
        {

        }

        private void label22_Click(object sender, EventArgs e)
        {

        }

        private void label21_Click(object sender, EventArgs e)
        {

        }

        private void label20_Click(object sender, EventArgs e)
        {

        }

        private void label19_Click(object sender, EventArgs e)
        {

        }

        private void label18_Click(object sender, EventArgs e)
        {

        }

        private void label17_Click(object sender, EventArgs e)
        {

        }

        private void label16_Click(object sender, EventArgs e)
        {

        }

        private void label15_Click(object sender, EventArgs e)
        {

        }

        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void label13_Click(object sender, EventArgs e)
        {

        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void label10_Click(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                foreach (SubGroup x in groupList)
                {
                    if (x.GroupID == Convert.ToInt32(textBox2.Text))
                    {
                        int result = floatList[x.FloatPlacement].numberOfRiders + Convert.ToInt32(textBox1.Text);
                        int addend = Convert.ToInt32(textBox1.Text);

                        if (result < 20 | result > 40)
                        {
                            MessageBox.Show("Invalid total number of members on this float.", "ERROR",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        if (x.AddMembers(addend))
                        {
                            floatList[x.FloatPlacement].numberOfRiders += addend;
                            label18.Text = Convert.ToString(x.numberInGroup);
                            changesMade = true;
                            update();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Groups are required to have between 8 and 20 members.", "ERROR",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                }
                SubGroup newGroup = new SubGroup(Convert.ToInt32(textBox1.Text));
                MessageBox.Show("Group does not exist, will be created as group number " + newGroup.GroupID, "Message");
                groupList.Add(newGroup);
            }
        }

        private void total_label_Click(object sender, EventArgs e)
        {
        }
    }
}
